package com.shopping.mall.core.module.attributeKey.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.attributeKey.entity.AttributeKey;
import com.shopping.mall.core.module.attributeKey.service.AttributeKeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "AttributeKey")
@RestController
@RequestMapping("/attributeKey")
public class AttributeKeyController {
    @Autowired
    private AttributeKeyService attributeKeyService;

//    @PostMapping("/searchByGoodsId")
//   todo   @ApiOperation("通过GoodsId")
//

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<AttributeKey>> getAttributeKeyPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        AttributeKey attributeKey) {
        IPage<AttributeKey> attributeKeyPage=attributeKeyService.getAttributeKeyPage(current,size,attributeKey);
        return Result.OK(attributeKeyPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<AttributeKey> update(@Validated(AttributeKey.update.class) @RequestBody AttributeKey attributeKey) {
        attributeKeyService.updateById(attributeKey);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(AttributeKey.insert.class) @RequestBody AttributeKey attributeKey) {
        attributeKeyService.save(attributeKey);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<AttributeKey> getById(@RequestParam String id){
        AttributeKey attributeKey=attributeKeyService.getById(id);
        return Result.OK(attributeKey);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<AttributeKey>> getAll(AttributeKey attributeKey){
        List<AttributeKey> attributeKeyList=attributeKeyService.getAll(attributeKey);
        return Result.OK(attributeKeyList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return attributeKeyService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return attributeKeyService.batchDelete(ids);
    }
}
